<template>
  <el-dialog
    title="新增员工"
    :visible="employeeIsShow"
    width="40%"
    top="8vh"
    @close="handleShutdown"
    @open="handleOpen"
  >
    <el-form
      ref="employerForm"
      label-width="120px"
      :rules="rules"
      :model="newEmployerObj"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="newEmployerObj.username"
          style="width: 80%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="newEmployerObj.mobile"
          style="width: 80%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="newEmployerObj.timeOfEntry"
          style="width: 80%"
          placeholder="请选择入职时间"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="newEmployerObj.formOfEmployment"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="newEmployerObj.workNumber"
          style="width: 80%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="newEmployerObj.departmentName"
          style="width: 80%"
          placeholder="请选择部门"
          @focus="showTree = true"
          readonly
        />
        <!-- 部门树形组件 -->
        <el-tree
          v-if="showTree"
          :data="deptTreeData"
          :props="{ label: 'name' }"
          @node-click="handleNodeClick"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="newEmployerObj.correctionTime"
          style="width: 80%"
          placeholder="请选择转正时间"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleShutdown">取 消</el-button>
      <el-button type="primary" @click="addEmployee">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import Obj from "@/constant/employees";
import { getDepartmentApi } from "@/api/departments";
import { addEmployeeApi } from "@/api/employees";
import { list2Tree } from "@/utils/shift";
export default {
  name: "AddEmployee",
  props: {
    employeeIsShow: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 收集员工信息
      newEmployerObj: {
        username: "", // 用户名
        mobile: "", // 手机号
        formOfEmployment: 1, // 聘用形式
        workNumber: "", // 工号
        departmentName: "", // 部门
        timeOfEntry: "", // 入职时间
        correctionTime: "", // 转正时间
      },
      // 校验规则
      rules: {
        username: [
          {
            required: true,
            message: "用户姓名不能为空",
            trigger: ["blur", "change"],
          },
          {
            min: 1,
            max: 4,
            message: "用户姓名为1-4位",
            trigger: ["blur", "change"],
          },
        ],
        mobile: [
          {
            required: true,
            message: "手机号不能为空",
            trigger: "blur",
          },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          {
            required: true,
            message: "聘用形式不能为空",
            trigger: ["blur", "change"],
          },
        ],
        workNumber: [
          {
            required: true,
            message: "工号不能为空",
            trigger: ["blur", "change"],
          },
        ],
        departmentName: [
          {
            required: true,
            message: "部门不能为空",
            trigger: "change",
          },
        ],
        timeOfEntry: [
          {
            required: true,
            message: "请选择入职时间",
            trigger: ["blur", "change"],
          },
        ],
      },
      // 聘用形式列表
      hireType: Obj.hireType,
      // 树形结构数据
      deptTreeData: [],
      // 控制部门树形组件隐藏和显示
      showTree: false,
    };
  },
  methods: {
    // 关闭弹层
    handleShutdown() {
      this.$refs.employerForm.resetFields();
      this.$emit("update:employeeIsShow", false);
    },
    // 新增员工
    addEmployee() {
      this.$refs.employerForm.validate(async (valia) => {
        if (!valia) return;
        await addEmployeeApi(this.newEmployerObj);
        // 通知父组件重新渲染
        this.$emit("Re-render");
        // 调用关闭弹层
        this.handleShutdown();
      });
    },
    async handleOpen() {
      const { data } = await getDepartmentApi();
      this.deptTreeData = list2Tree(data.depts, "");
    },
    handleNodeClick(node) {
      if (node.children) return;
      this.newEmployerObj.departmentName = node.name;
      this.showTree = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.el-tree {
  position: absolute;
  width: 80%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>